/**
 * Created by Administrator on 2017/1/11 0011.
 */

import {Http,Headers,RequestOptions}from "@angular/http";
import 'rxjs/add/operator/map';
import {Observable} from "rxjs";

export class BaseService {
  public HOST_URL = 'http://api.visoon.net/';

  constructor( public http: Http){
  }

  public getWithoutData(url): Observable<any> {
    let headers = new Headers();
    // headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({headers: headers});
    return this.http.get(this.HOST_URL + url, options).map(res => res.json());
  }

  public get(url, dataString): Observable<any> {
    let token = localStorage['token'] || '';
    let headers = new Headers();
    // headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({headers: headers});
    return this.http.get(this.HOST_URL + url + "?token=" + token + "&" + dataString, options).map(res => res.json());
  }

  public getWithoutToken(url, dataString): Observable<any> {
    let headers = new Headers();
    // headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({headers: headers});
    return this.http.get(this.HOST_URL + url + "?" + dataString, options).map(res => res.json());
  }

  public post(url, body): Observable<any> {
    body['token'] = localStorage['token'];
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});
    return this.http.post(this.HOST_URL + url, body, options).map(res => res.json());
  }
}
